<template>
  <div>
    <div class="header-img">
      <span class="header-arrow" @click="go">
        <van-icon color="rgba(0,0,0,.2)" name="arrow-left" />
      </span>
      <img :src="detail.handcover" alt="">
    </div>
    <div class="course-detail-m">
      <div class="course-detail">课程名称: {{detail.courseName}}</div>
      <div class="course-detail">所属机构: {{detail.orgName}}</div>
      <div class="course-detail">课程教师: {{detail.teacherName}}</div>
      <div class="course-detail">课程价格: <span style="color: #ff4444">￥{{detail.price}}</span></div>
      <div class="course-detail">课程简介: </div>
      <div style="text-indent: 1em" v-html="detail.introduction"></div>
      <div>课程列表:</div>
      <div v-if="liveType === '2'" style="text-indent: 1em" v-for="(item,index) in detail.chaptersListVOS">
        <p>{{index + 1}}.{{item.chapterName}}</p>
        <p style="text-indent: 2em" v-for="(items,indexs) in item.genseesList">{{index + 1}}.{{indexs + 1}}.{{items.genseeTitle}}</p>
      </div>
      <div v-if="liveType === '1'" style="text-indent: 1em">
        <p>{{detail.aliyunLives.livename}}</p>
      </div>
      <div v-if="liveType === '0'" style="text-indent: 1em" v-for="(item,index) in detail.chaptersListVOS">
        <p>{{index + 1}}.{{item.chapterName}}</p>
        <p style="text-indent: 2em" v-for="(items,indexs) in item.videoDemandList">{{index + 1}}.{{indexs + 1}}.{{items.videoName}}</p>
      </div>
    </div>
    <div style="height: 50px"></div>
    <van-button class="btn-danger" @click="btnOrder" type="danger">{{order}}</van-button>
  </div>
</template>

<script>
  export default {
    name: 'course-detail',
    data() {
      return {
        detail: {},
        liveType: '',
        id: '',
        order: '去购买'
      }
    },
    mounted() {
      this.type()
      this.findCourse()
    },
    methods: {
      type() {
        this.id = this.$route.query.id
        this.liveType = this.$route.query.type.toString()
        if (this.liveType === '0') {
          this.earchDetail('/api/api/student/luCourse/detail', this.id)
        } else if (this.liveType === '1') {
          this.earchDetail('/api/api/student/liveCourse/detail', this.id)
        } else {
          this.earchDetail('/api/api/student/course/detail', this.id)
        }
      },
      earchDetail(url, id) {
        this.$get(url, {courseId: id}).then((res) => {
          if (res.code === 0) {
            this.detail = res.data
          }
        })
      },
      findCourse() {
        this.$get('/api/order/findCourse', {courseId: this.id}).then((res) => {
          if (res.code === 0) {
            if (res.data === '1') {
              this.order = '去学习'
            }
          }
        })
      },
      go() {
        this.$router.go(-1)
      },
      btnOrder() {
        if (this.order === '去学习') {
          this.$router.push({path: '/index/learning', query: {id: this.id, type: this.liveType}})
        } else {
          this.$dialog.confirm({
            title: '你确定购买该课程？',
            confirmButtonText: '去购买'
          }).then(() => {
            this.$post('/api/order/create', {courseId: this.id, orderRemarks: 'h5'}).then((res) => {
              console.log(res)
              if (res.code === 0) {
                this.$router.push({path: '/index/order-pay', query: {orderId: res.data}})
              }
            })
          })
        }
      }
    }
  }
</script>

<style scoped>
  .course-detail-m{
    font-size: 28px;
    margin: 30px;
  }
  .course-detail{
    margin: 5px 0;
  }
  .header-img{
    width: 100%;
    height: 480px;
  }
  .header-img img{
    width: 100%;
    height: 100%;
  }
  .header-arrow{
    position: absolute;
    left: 20px;
    top: 20px;
    font-size: 70px;
    text-align: center;
    line-height: 70px;
    border-radius: 50%;
    height: 70px;
    width: 70px;
    background: rgba(255,255,255,.3);
  }
  .btn-danger{
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
  }
</style>
